package com.skivingcloud.camunda.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.camunda.entity.WfStrategy;
import com.skivingcloud.camunda.mapper.WfStrategyMapper;
import com.skivingcloud.camunda.service.WfStrategyService;
import com.skivingcloud.common.utils.PageParameter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审批人找人策略 服务层实现。
 *
 * @author skivingcloud
 * @since 2024-05-17
 */
@Service
public class WfStrategyServiceImpl extends ServiceImpl<WfStrategyMapper, WfStrategy> implements WfStrategyService {

    /**
     * 分页查询审批人找人策略
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<WfStrategy> queryByPage(PageParameter<WfStrategy> parameter){
        Page<WfStrategy> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if(StringUtils.isNotBlank(parameter.getParam().getStrategyName())){
            queryWrapper.and(WfStrategy::getStrategyName).like(parameter.getParam().getStrategyName());
        }
        if(StringUtils.isNotBlank(parameter.getParam().getStrategyType())){
            queryWrapper.and(WfStrategy::getStrategyType).eq(parameter.getParam().getStrategyType());
        }
        return this.page(page, queryWrapper);
    }
    
    /**
     * 查询策略{key，value}
     *
     * @return 策略信息
     */
    @Override
    public List<Map<String, Object>> getWfStrategyKeyValue(){
        QueryWrapper queryWrapper = QueryWrapper.create();
        List<WfStrategy> strategyList = this.list(queryWrapper);
        return strategyList.stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("key", m.getId());
            map.put("value", m.getStrategyName());
            return map;
        }).toList();
    }
}
